package com.wandian.texas.command.texas;

import java.util.concurrent.TimeUnit;

import com.wandian.command.Cmd;
import com.wandian.command.UserCommand;
import com.wandian.executor.TimerCommand;
import com.wandian.texas.BaseCommandObject;
import com.wandian.texas.GameData;
import com.wandian.texas.GamePool;
import com.wandian.texas.GameServer;
import com.wandian.texas.ThreadPool;
import com.wandian.texas.protocol.Protocol;
import com.wandian.xml.RoomConfigItem;

/**
 * @author long
 * @Create 2012-10-16 下午4:31:12
 * @QQ 108232706
 * @version 1.0
 * @description DelayMessageCmd
 */
@Cmd(code = Protocol.CHATROOM_DELAY_MESSAGE, desc = "")
public class DelayMessageCmd extends UserCommand {
	@Override
	public void execute(BaseCommandObject cmdObject) {
		super.execute(cmdObject);
		TimerCommand cmd = (TimerCommand) cmdObject;
		if (cmd == null)
			return;
		int type = (Integer) cmd.get("Type");
		switch (type) {
		// case 10:
		// GameServer.cur_port();
		// break;
		// case 11:
		// GameServer.show();
		// break;
		// case 12:
		// GameServer.ports();
		// break;
		// case 13:
		// GameServer.gameover();
		// break;
		case GameData.TIMERTOFOLD:
			// timeroutFold(cmd);
			break;
		case GameData.TIMERTOOPERATIONNEXT:
			operationNext(cmd);
			break;
		case GameData.SHOWBONUSPOOL:
			SHOWBONUSPOOL(cmd);
			break;
		case GameData.SHOWCARD:
			SHOWCARD(cmd);
			break;
		case GameData.CMD_POTS:
			CMD_POTS(cmd);
			break;
		case GameData.BROADCASTGAMEOVER:
			BROADCASTGAMEOVER(cmd);
			break;
		case GameData.STARTNEWGAME:
			STARTNEWGAME(cmd);
			break;
		case GameData.CMD_SNAPCARDS_FLOP:
			CMD_SNAPCARDS_FLOP(cmd);
			break;
		case GameData.CMD_SNAPCARDS_TURN:
			CMD_SNAPCARDS_TURN(cmd);
		case GameData.CMD_SNAPCARDS_RIVER:
			CMD_SNAPCARDS_RIVER(cmd);
			break;
		case GameData.CMD_CORNUCOPIA:
			CMD_CORNUCOPIA(cmd);
			break;
		case GameData.CMD_RECONNECT:
			GameServer.requestor.RECONNECT(cmd);
			break;
		default:
			break;
		}

	}

	private void CMD_SNAPCARDS_TURN(TimerCommand cmd) {
		RoomConfigItem room = (RoomConfigItem) cmd.get("RoomConfigItem");
		BaseCommandObject cmdObject =ThreadPool.getInstance().takeFreeCommand(cmd);
		GamePool.CMD_SNAPCARDS_TURN(room, cmdObject);
		room.delay(1500, TimeUnit.MILLISECONDS);
		room.startTimer(GameData.CMD_SNAPCARDS_RIVER);
	}

	private void CMD_SNAPCARDS_RIVER(TimerCommand cmd) {
		RoomConfigItem room = (RoomConfigItem) cmd.get("RoomConfigItem");
		BaseCommandObject cmdObject =ThreadPool.getInstance().takeFreeCommand(cmd);
		GamePool.CMD_SNAPCARDS_RIVER(room, cmdObject);
		room.delay(1500, TimeUnit.MILLISECONDS);
		room.startTimer(GameData.SHOWBONUSPOOL, room, cmdObject);
	}

	private void CMD_SNAPCARDS_FLOP(TimerCommand cmd) {
		RoomConfigItem room = (RoomConfigItem) cmd.get("RoomConfigItem");
		BaseCommandObject cmdObject =ThreadPool.getInstance().takeFreeCommand(cmd);
		GamePool.CMD_SNAPCARDS_FLOP(room, cmdObject);
		room.startTimer(GameData.CMD_SNAPCARDS_TURN);
	}

	private void STARTNEWGAME(TimerCommand cmd) {
		RoomConfigItem room = (RoomConfigItem) cmd.get("RoomConfigItem");
		BaseCommandObject cmdObject =ThreadPool.getInstance().takeFreeCommand(cmd);
		GamePool.STARTNEWGAME(room, cmdObject);
	}

	private void BROADCASTGAMEOVER(TimerCommand cmd) {
		RoomConfigItem room = (RoomConfigItem) cmd.get("RoomConfigItem");
		BaseCommandObject cmdObject =ThreadPool.getInstance().takeFreeCommand(cmd);
		GamePool.broadCastGameOver(room, cmdObject);
		boolean needWait = false;
		if (needWait) {
			room.delay(100, TimeUnit.MILLISECONDS);
			room.startTimer(GameData.STARTNEWGAME);
		} else {
			STARTNEWGAME(cmd);
		}
	}

	private void CMD_POTS(TimerCommand cmd) {
		RoomConfigItem room = (RoomConfigItem) cmd.get("RoomConfigItem");
		int curPot = (Integer) cmd.get("CurPot");
		BaseCommandObject cmdObject =ThreadPool.getInstance().takeFreeCommand(cmd);
		GamePool.CMD_POTS(room, cmdObject, curPot);
		int potLen = room.BonusPoolsSize();
		curPot++;
		if (curPot >= potLen) {
			room.delay(3000, TimeUnit.MILLISECONDS);
			room.startTimer(GameData.BROADCASTGAMEOVER);
		} else {
			room.putTimerParams("CurPot", curPot);
			room.delay(1500, TimeUnit.MILLISECONDS);
			room.startTimer(GameData.CMD_POTS);
		}
	}

	/**
	 * 聚宝盆
	 * 
	 * @param cmd
	 */
	private void CMD_CORNUCOPIA(TimerCommand cmd) {
		RoomConfigItem room = (RoomConfigItem) cmd.get("RoomConfigItem");
		BaseCommandObject cmdObject =ThreadPool.getInstance().takeFreeCommand(cmd);
		// 处理聚宝盆
		GamePool.CMD_REWARD_CORNUCOPIA(room, cmdObject);
		// GamePool.CMD_TESTREWARD_CORNUCOPIA(room, cmdObject);
		// if(room.gameRoom.Cornucopias.size()>0){
		room.delay(3000, TimeUnit.MILLISECONDS);
		room.startTimer(GameData.CMD_POTS);
	}

	/**
	 * 游戏结束，显示底牌(仅剩一人则直接显示赢家)(不翻牌，积宝算不算?)
	 * @param cmd
	 */
	private void SHOWCARD(TimerCommand cmd) {
		RoomConfigItem room = (RoomConfigItem) cmd.get("RoomConfigItem");
		BaseCommandObject cmdObject =ThreadPool.getInstance().takeFreeCommand(cmd);
		boolean showCard = (Boolean) cmd.get("ShowCard");
		int dbIndex = (Integer) cmd.get("DbIndex");
		GamePool.SHOWCARD(room, cmdObject, showCard, dbIndex);
		room.putTimerParams("CurPot", 0);
		if (showCard) {
			// 中心服务器允许使用积宝，并且积宝在本局已使用
//			if (room.isAllowCornucopia() && room.isUsingCornucopia()) {
//				if (room.gameRoom.addCornucopiaSuc && room.gameRoom.Cornucopias.size() > 0) {
//					// 新建一个命令，去与中心服务器通信，告诉产生了积宝，同时发送更新当前积宝到客户端
//					// room.delay(1000, TimeUnit.MILLISECONDS);
//					// room.startTimer(GameData.CMD_CORNUCOPIA);
//					CornucopiaEntity cornucopia = room.gameRoom.Cornucopias.get(0);
//					GameServer.requestor.requestSendCornucopia(true, room.cornucopiaCallBack(), room.cornucopiaCardType(), room.id,room.cornucopiaSeatId(), cornucopia.UserId,cornucopia.idx, cornucopia.NickName,cornucopia.MaxCards);
//				} else {
////					Log.error("中心服务器允许使用积宝，并且积宝在本局已使用,但是游戏结束却没有出现积宝牌");
//				}
//			} else {
//				if (room.gameRoom.Cornucopias.size() > 0) {//room.gameRoom.addCornucopiaSuc
//					throw new IllegalArgumentException("中心服务器不允许使用积宝或者积宝在本局已使用,但是游戏结束却出现积宝牌");
////					Log.error("中心服务器不允许使用积宝或者积宝在本局已使用,但是游戏结束却出现积宝牌");
//				}
//			}
			// //test--聚宝盆产生
			// room.delay(1000, TimeUnit.MILLISECONDS);
			// room.startTimer(GameData.CMD_CORNUCOPIA);
			room.delay(3000, TimeUnit.MILLISECONDS);
			room.startTimer(GameData.CMD_POTS);

		} else {
			// 中心服务器允许使用积宝，并且积宝在本局已使用，但是没有翻牌，扔弃该积宝
//			if (room.isAllowCornucopia() && room.isUsingCornucopia()) {
//				if (room.gameRoom.Cornucopias.size() > 0) {//room.gameRoom.addCornucopiaSuc && 
//					// 新建一个命令，去与中心服务器通信，告诉产生了积宝，同时发送更新当前积宝到客户端
//					// room.delay(1000, TimeUnit.MILLISECONDS);
//					// room.startTimer(GameData.CMD_CORNUCOPIA);
//					CornucopiaEntity cornucopia = room.gameRoom.Cornucopias.get(0);
//					GameServer.requestor.requestSendCornucopia(false, room.cornucopiaCallBack(), room.cornucopiaCardType(), room.id,room.cornucopiaSeatId(), cornucopia.UserId,cornucopia.idx, cornucopia.NickName,cornucopia.MaxCards);
//				} else {
//					throw new IllegalArgumentException("中心服务器允许使用积宝，并且积宝在本局已使用,但是游戏结束却没有出现积宝牌");
//				}
//			}
			CMD_POTS(cmd);
		}
	}

	private void SHOWBONUSPOOL(TimerCommand cmd) {
		RoomConfigItem room = (RoomConfigItem) cmd.get("RoomConfigItem");
		BaseCommandObject cmdObject =ThreadPool.getInstance().takeFreeCommand(cmd);
		boolean boolGameOver = (Boolean) cmd.get("BoolGameOver");
		GamePool.ShowBonusPool(room, cmdObject);
		if (!boolGameOver) {
			room.delay(200, TimeUnit.MILLISECONDS);
			room.startTimer(GameData.TIMERTOOPERATIONNEXT, room, cmdObject);
		} else {
			room.delay(1800, TimeUnit.MILLISECONDS);
			room.startTimer(GameData.SHOWCARD);
		}

	}

	/**
	 * 延时显示当前操牌
	 * 
	 * @param cmd
	 */
	private void operationNext(TimerCommand cmd) {
		RoomConfigItem room = (RoomConfigItem) cmd.get("RoomConfigItem");
		BaseCommandObject cmdObject =ThreadPool.getInstance().takeFreeCommand(cmd);//(BaseCommandObject) cmd.get("BaseCommandObject");
		GamePool.operationNext(room, cmdObject);
	}
}
